import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  ManyToOne,
  JoinColumn,
  OneToMany,
  BeforeUpdate,
  BeforeInsert,
} from 'typeorm';
import { Category } from 'src/modules/category/entities/category.entity';
import { Stage } from 'src/modules/stage/entities/stage.entity';
import { EventToPlayer } from 'src/modules/event-to-player/entities/event-to-player.entity';
import { PlayerSignup } from 'src/modules/player-signup/entities/player-signup.entity';
import { EventStatusEnum, EventTypeEnum } from 'src/common/constant/enums';
import { Tournament } from 'src/modules/tournament/entities/tournament.entity';

@Entity('event')
export class Event {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  name: string;

  @Column()
  type: EventTypeEnum;

  @Column()
  fee: number;

  @Column()
  minPlayerCount: number;

  @Column()
  maxPlayerCount: number;

  @Column()
  createBy: number;

  @Column()
  createTime: Date;

  @Column()
  updateBy: number;

  @Column()
  updateTime: Date;

  @OneToMany(() => EventToPlayer, (eventToPlayer) => eventToPlayer.event, {
    cascade: true,
  })
  eventToPlayers: EventToPlayer[];

  @OneToMany(() => PlayerSignup, (playerSignup) => playerSignup.event)
  playerSignups: PlayerSignup[];

  @ManyToOne(() => Category, (category) => category.events)
  @JoinColumn({ name: 'fk_category_id' })
  category: Category;

  @Column({ name: 'fk_category_id' })
  categoryId: number;

  @OneToMany(() => Stage, (stage) => stage.event, {
    cascade: true,
  })
  stages: Stage[];

  @BeforeInsert()
  BeforeInsert() {
    this.createTime = new Date();
    this.updateTime = this.createTime;
  }

  @BeforeUpdate()
  BeforeUpdate() {
    this.updateTime = new Date();
  }

  @Column({ name: 'fk_tournamentId' })
  tournamentId: number;

  @ManyToOne(() => Tournament)
  @JoinColumn({ name: 'fk_tournamentId' })
  tournament: Tournament;

  @Column()
  status: EventStatusEnum;
}
